package edu.hawaii.webspider;

import junit.framework.TestCase;

/**
 * Test case for the WebSpiderExample class.
 * 
 * @author Laura Matsuo
 *
 */
public class TestWebSpiderExample extends TestCase {
  private static String totalLinks = "-totallinks";
  private static String mostPopular = "-mostpopular";
  private static String logging = "-logging";
  private static String myUrl = "http://www2.hawaii.edu/~lauramat/myfavorites/";
  
  /**
   * Tests the normal operation of the Web Spider.
   * 
   * @throws  Exception  If invalid URL.
   */
  public void testNormalOperation() throws Exception {
    WebSpiderExample webSpider = new WebSpiderExample("http://www2.hawaii.edu/~lauramat/");
    assertEquals("Check that only one link is counted", 1, webSpider.getNumLinks());
    webSpider = new WebSpiderExample("http://www2.hawaii.edu/~lauramat/", 1);
    assertEquals("Check that only one link is counted", 1, webSpider.getNumLinks());
  }
  
  /**
   * Tests getNumLinks method with an invalid URL.
   */
  public void testInvalidUrl() {
    WebSpiderExample webSpider = new WebSpiderExample("http://hello");
    try {
      webSpider.getNumLinks();
      fail("Illegal URL did not generate exception.");
    }
    catch (Exception e) {
      // shouldn't get here.
      System.out.println("testInvalidUrl caught the following exception: " + e);
    }
  }
  
  /**
   * Tests that the main method runs.
   */
  public void testMain() {
    String[] testing = {"testing"};
    try {
      WebSpiderExample.main(testing);
    }
    catch (Exception e) {
      fail("An error occurred in the main method" + e);
    }
  }
  
  /**
   * Tests that the starting URL and number of pages to crawl through get set when creating an
   * empty WebSpiderExample object.
   */
  public void testSetUp() {
    WebSpiderExample webSpider = new WebSpiderExample();
    String startUrl = "http://www.hawaii.edu";
    int numPages = 10;
    webSpider.setUpWebSpider(startUrl, numPages);
    assertEquals("Check that the start page got set", startUrl, webSpider.getStartUrl());
    assertEquals("Check that the number of pages got set", numPages, webSpider.getNumPages());
  }
  
  /**
   * Tests that no links are counted on a page with no links.
   */
  public void testNoLinks() {
    int totalLinks = 0;
    WebSpiderExample webSpider = 
      new WebSpiderExample("http://www2.hawaii.edu/~lauramat/page2.html", 1);
    
    try {
      webSpider.getTotalLinks("http://www2.hawaii.edu/~lauramat/page2.html");
      totalLinks = webSpider.sumNumberLinks();
      assertEquals("Check that 0 links were found", 0, totalLinks);
    }
    catch (Exception e) {
      System.out.println("testNoLinks caught the following exception: " + e);
    }
  }
  
  /**
   * Tests that an exception gets caught if the page is invalid.
   */
  public void testInvalidPage() {
    int totalLinks = 0;
    WebSpiderExample webSpider = 
      new WebSpiderExample("http://www2.hawaii.edu/~lauramat/alex/bin.jpg", 1);
    
    try {
      webSpider.getTotalLinks("http://www2.hawaii.edu/~lauramat/alex/bin.jpg");
      totalLinks = webSpider.sumNumberLinks();
      assertEquals("Check that 0 links were found", 0, totalLinks);
    }
    catch (Exception e) {
      System.out.println("testInvalidPage caught the following exception: " + e);
    }
  }
  
  /**
   * Tests that the different modes get set. 
   */
  public void testSetModes() {
    String[] args = {totalLinks, mostPopular, "http://msn.com", "1", logging};
    WebSpiderExample webSpider = new WebSpiderExample();
    webSpider.setModes(args);
    assertTrue("Total links mode was set", webSpider.getInTotalLinksMode());
    assertTrue("Most popular mode was set", webSpider.getInMostPopularMode());
    assertTrue("Logging mode was set", webSpider.getInLoggingMode());
  }
  
  /**
   * Tests that the program runs in total links mode.
   */
  public void testRunTotalLinks() {
    String[] args = {totalLinks, myUrl, "2"};
    WebSpiderExample webSpider = new WebSpiderExample();
    webSpider.setModes(args);
    webSpider.run();
    assertEquals("Check that ten links were found", 10, webSpider.sumNumberLinks());
    assertEquals("Check that starting number of pages is still 2", 2, 
        webSpider.getStartingNumPages());
  }
  
  /**
   * Tests that the program runs in logging mode.
   */
  public void testRunLogging() {
    String[] args = {totalLinks, myUrl, "2", logging};
    WebSpiderExample webSpider = new WebSpiderExample();
    webSpider.setModes(args);
    webSpider.run();
    assertTrue("In logging mode", webSpider.getInLoggingMode());
  }
  
  /**
   * Tests that the program runs in most popular mode.
   */
  public void testRunMostPopular() {
    String[] args = {mostPopular, myUrl, "2"};
    WebSpiderExample webSpider = new WebSpiderExample();
    webSpider.setModes(args);
    assertTrue("Most popular mode was set", webSpider.getInMostPopularMode());
    webSpider.run();
    assertEquals("Check the most popular URL", 
        "http://www2.hawaii.edu/~lauramat/myfavorites/movies.htm", webSpider.getMostPopularUrl());
    assertEquals("Check the most popular URL count", 2, webSpider.getMostPopularUrlCount());
  }
}
